package xworker.gluon.glisten.control;

import com.gluonhq.charm.glisten.control.Avatar;
import com.gluonhq.charm.glisten.control.AvatarPane;
import javafx.beans.property.ListProperty;
import javafx.scene.Node;
import javafx.util.Callback;
import org.xmeta.ActionContext;
import org.xmeta.Thing;
import xworker.javafx.beans.property.PropertyFactory;
import xworker.javafx.control.ControlActions;
import xworker.javafx.util.JavaFXUtils;
import xworker.lang.util.UtilAction;

public class AvatarPaneActions {
    static{
        PropertyFactory.regist(AvatarPane.class, "avatarFactoryProperty", o -> {
            AvatarPane<?> obj = (AvatarPane<?>) o;
            return obj.avatarFactoryProperty();
        });
        PropertyFactory.regist(AvatarPane.class, "contentFactoryProperty", o -> {
            AvatarPane<?> obj = (AvatarPane<?>) o;
            return obj.contentFactoryProperty();
        });
        PropertyFactory.regist(AvatarPane.class, "itemsProperty", o -> {
            AvatarPane<?> obj = (AvatarPane<?>) o;
            return obj.itemsProperty();
        });
        PropertyFactory.regist(AvatarPane.class, "valueProperty", o -> {
            AvatarPane<?> obj = (AvatarPane<?>) o;
            return obj.valueProperty();
        });
        PropertyFactory.regist(AvatarPane.class, "expandedProperty", o -> {
            AvatarPane<?> obj = (AvatarPane<?>) o;
            return obj.expandedProperty();
        });
        PropertyFactory.regist(AvatarPane.class, "exitActionProperty", o -> {
            AvatarPane<?> obj = (AvatarPane<?>) o;
            return obj.exitActionProperty();
        });
        PropertyFactory.regist(AvatarPane.class, "collapsibleProperty", o -> {
            AvatarPane<?> obj = (AvatarPane<?>) o;
            return obj.collapsibleProperty();
        });
    }

    public static void init(AvatarPane<Object> node, Thing thing, ActionContext actionContext){
        ControlActions.init(node, thing, actionContext);

        Callback<Object, Avatar> avatarFactory = JavaFXUtils.getObject(thing, "avatarFactory", actionContext);
        if(avatarFactory != null){
            node.setAvatarFactory(avatarFactory);
        }

        if(thing.valueExists("collapsible")){
            node.setCollapsible(thing.getBoolean("collapsible"));
        }

        Callback<Object, Node> contentFactory = JavaFXUtils.getObject(thing, "contentFactory", actionContext);
        if(contentFactory != null){
            node.setContentFactory(contentFactory);
        }

        Runnable exitAction = JavaFXUtils.getObject(thing, "exitAction", actionContext);
        if(exitAction != null){
            node.setExitAction(exitAction);
        }

        if(thing.valueExists("expanded")){
            node.setExpanded(thing.getBoolean("expanded"));
        }

        ListProperty<Object> items = JavaFXUtils.getObject(thing, "items", actionContext);
        if(items != null){
            node.setItems(items);
        }

        Object value = JavaFXUtils.getObject(thing, "value", actionContext);
        if(value != null){
            node.setValue(value);
        }
    }

    public static AvatarPane<Object> create(ActionContext actionContext){
        Thing self = actionContext.getObject("self");
        AvatarPane<Object> node = new AvatarPane<>();

        init(node, self, actionContext);
        actionContext.peek().put("parent", node);
        for(Thing child : self.getChilds()){
            child.doAction("create", actionContext);
        }

        actionContext.g().put(self.getMetadata().getName(), node);

        return node;
    }
    
    public static void createAvatarFactory(ActionContext actionContext){
        Thing self = actionContext.getObject("self");
        AvatarPane<Object> node = actionContext.getObject("parent");
        
        node.setAvatarFactory(param -> UtilAction.runChildsAsAction(self, actionContext, "value", param));
    }

    public static void createContentFactory(ActionContext actionContext){
        Thing self = actionContext.getObject("self");
        AvatarPane<Object> node = actionContext.getObject("parent");

        node.setContentFactory(param -> UtilAction.runChildsAsAction(self, actionContext, "value", param));
    }

    public static void createExitAction(ActionContext actionContext){
        Thing self = actionContext.getObject("self");
        AvatarPane<Object> node = actionContext.getObject("parent");

        node.setExitAction(() -> UtilAction.runChildsAsAction(self, actionContext));
    }

    public static void createItemsFactory(ActionContext actionContext){
        Thing self = actionContext.getObject("self");
        AvatarPane<Object> node = actionContext.getObject("parent");

        ListProperty<Object> items = UtilAction.runChildsAsAction(self, actionContext);
        if(items != null){
            node.setItems(items);
        }
    }
}
